package com.xmu.edu.tisp.service.contract;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.xmu.edu.tisp.dao.Service;
import com.xmu.edu.tisp.dao.TISP_dbHelper;

public class XJSZ_ZYMD_DETAIL_service implements Service {

	public static final String TABLE_NAME = "XJSZ_ZYMD_DETAIL";
	public static final String PRIMARY_KEY = "XJSZ_ZYMD_DETAIL_ID";

	private SQLiteDatabase myDB;
	private SQLiteOpenHelper dbHelper;

	public XJSZ_ZYMD_DETAIL_service(Context cxt) {
		dbHelper = new TISP_dbHelper(cxt, "TISP");
	}

	public SQLiteDatabase getdb() {
		if (myDB == null) {
			myDB = dbHelper.getWritableDatabase();
			return myDB;
		}
		return myDB;
	}

	@Override
	public void g_updata(ContentValues values) {
		getdb().update(TABLE_NAME, values, PRIMARY_KEY + "=?",
				new String[] { (String) values.get(PRIMARY_KEY) });
		System.out.println(TABLE_NAME + "have updataed!");

	}

	@Override
	public void g_insert(ContentValues values) {
		getdb().insert(TABLE_NAME, null, values);

		System.out.println(TABLE_NAME + "have inserted!");
	}

	@Override
	public void g_delete(String[] id) {
		getdb().delete(TABLE_NAME, PRIMARY_KEY + "=?", id);
		System.out.println(TABLE_NAME + "have deleted!");

	}

	@Override
	public List<String> g_getIDs() {
		Cursor c = getdb().query(TABLE_NAME, new String[] { PRIMARY_KEY },
				null, null, null, null, null);
		List<String> result = new ArrayList<String>();
		while (c.moveToNext()) {
			String id = c.getString(c.getColumnIndex(PRIMARY_KEY));
			result.add(id);
		}
		return result;
	}

	public long insertData(String xjsz_zymd_detail_id, String xjsz_zymd_id,
			String xh, String field_block_id, String pzdm, String yxzzmj,
			String yxjssl) {
		ContentValues initialValues = new ContentValues();

		initialValues.put("XJSZ_ZYMD_DETAIL_ID", xjsz_zymd_detail_id);

		initialValues.put("XJSZ_ZYMD_ID", xjsz_zymd_id);

		initialValues.put("XH", xh);

		initialValues.put("FIELD_BLOCK_ID", field_block_id);

		initialValues.put("PZDM", pzdm);

		initialValues.put("YXZZMJ", yxzzmj);

		initialValues.put("YXJSSL", yxjssl);

		return getdb().insert(TABLE_NAME, null, initialValues);
	}

	public Cursor fetchDataByXJSZ_ZYMD_ID(String XJSZ_ZYMD_ID) {

		Cursor cursor = getdb()
				.rawQuery(
						"select XJSZ_ZYMD_DETAIL_ID,FIELD_BLOCK_CODE,FIELD_MANAGER,LEAF_VARIETY_NAME,PZDM "
								+ "from TP_FIELD_BLOCK,CM_LEAF_VARIETY,XJSZ_ZYMD_DETAIL "
								+ "where TP_FIELD_BLOCK.FIELD_BLOCK_ID=XJSZ_ZYMD_DETAIL.FIELD_BLOCK_ID "
								+ " and CM_LEAF_VARIETY.LEAF_VARIETY_CD=XJSZ_ZYMD_DETAIL.PZDM "
								+ " and XJSZ_ZYMD_DETAIL.XJSZ_ZYMD_ID=?;",
						new String[] { XJSZ_ZYMD_ID });

		cursor.deactivate();
		return cursor;
	}

	public Cursor fetchDataByXJSZ_ZYMD_DETAIL_ID(String XJSZ_ZYMD_DETAIL_ID) {

		System.out.println(XJSZ_ZYMD_DETAIL_ID);
		Cursor cursor = getdb().query(
				TABLE_NAME,
				new String[] { "XJSZ_ZYMD_DETAIL_ID", "XJSZ_ZYMD_ID", "XH",
						"FIELD_BLOCK_ID", "PZDM", "YXZZMJ", "YXJSSL" },
				"XJSZ_ZYMD_DETAIL_ID=?", new String[] { XJSZ_ZYMD_DETAIL_ID },
				null, null, null, null);

		cursor.deactivate();
		return cursor;

	}
	public Cursor fetchFieldAreaDataByXJSZ_ZYMD_ID(String XJSZ_ZYMD_ID) {

		Cursor cursor = getdb()
				.rawQuery(
						"select FIELD_BLOCK_ATEA "
								+ "from TP_FIELD_BLOCK,XJSZ_ZYMD_DETAIL "
								+ "where TP_FIELD_BLOCK.FIELD_BLOCK_ID=XJSZ_ZYMD_DETAIL.FIELD_BLOCK_ID "
								
								+ " and XJSZ_ZYMD_DETAIL.XJSZ_ZYMD_ID=?;",
						new String[] { XJSZ_ZYMD_ID });

		cursor.deactivate();
		return cursor;
	}
	
	public Cursor fetchDataListByXJSZ_ZYMD_ID(String XJSZ_ZYMD_ID){
		Cursor cursor = getdb()
		.rawQuery(
				"select * "
						+ "from XJSZ_ZYMD_DETAIL "
						+ "where XJSZ_ZYMD_DETAIL.XJSZ_ZYMD_ID=? ",
				new String[] { XJSZ_ZYMD_ID });

        cursor.deactivate();
        return cursor;
	}
	

	public boolean updateData(String XJSZ_ZYMD_DETAIL_ID,
			String xjsz_zymd_detail_id, String xjsz_zymd_id, String xh,
			String field_block_id, String pzdm, String yxzzmj, String yxjssl) {

		ContentValues args = new ContentValues();

		args.put("XJSZ_ZYMD_DETAIL_ID", xjsz_zymd_detail_id);
		args.put("XJSZ_ZYMD_ID", xjsz_zymd_id);
		args.put("XH", xh);
		args.put("FIELD_BLOCK_ID", field_block_id);
		args.put("PZDM", pzdm);
		args.put("YXZZMJ", yxzzmj);
		args.put("YXJSSL", yxjssl);
		return getdb().update(TABLE_NAME, args, "XJSZ_ZYMD_DETAIL_ID" + "=?",
				new String[] { XJSZ_ZYMD_DETAIL_ID }) > 0;
	}

	public boolean deleteData(String XJSZ_ZYMD_DETAIL_ID) {
		return getdb().delete(TABLE_NAME, "XJSZ_ZYMD_DETAIL_ID" + "=?",
				new String[] { XJSZ_ZYMD_DETAIL_ID }) > 0;

	}
	
	public boolean deleteDataByXJSZ_ZYMD_ID(String XJSZ_ZYMD_ID) {
		return getdb().delete(TABLE_NAME, "XJSZ_ZYMD_ID" + "=?",
				new String[] { XJSZ_ZYMD_ID }) > 0;

	}

}
